package com.easemob.chatuidemo.service;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;
import com.easemob.chatuidemo.domain.Nearby;
import com.easemob.chatuidemo.domain.User;
import com.easemob.chatuidemo.utils.FlyAntGlobalVar;
import com.easemob.chatuidemo.utils.JSONUtil;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UserService {
    private static final String TAG = "UserService";
    
    private static final String URL_REGISTER = "http://www.365jiaju.com/app/im/registration.ashx";
    private static final String URL_LOGIN = "http://www.365jiaju.com/app/im/login.ashx";
    private static final String URL_GETACCOUNT = "http://www.365jiaju.com/app/im/getAccount.ashx";
    private static final String URL_MODIFY = "http://www.365jiaju.com/app/im/modifyAccount.ashx";
    private static final String URL_UPDATEPWD = "http://www.365jiaju.com/app/im/updatepwd.ashx";
    private static final String URL_UPLOADUPIC = "http://www.365jiaju.com/app/im/uploadUpic.ashx";
    private static final String URL_CONTACTSINFO = "http://www.365jiaju.com/app/im/getContactsInfo.ashx";
    private static final String URL_FINDALL = "http://www.365jiaju.com/app/im/findall.ashx";
    private static final String URL_FINDDRIVER = "http://www.365jiaju.com/app/im/findsiji.ashx";
    private static final String URL_FINDWULIU = "http://www.365jiaju.com/app/im/findwuliu.ashx";
    private static final String URL_FINDMASTER = "http://www.365jiaju.com/app/im/findshifu.ashx";
    private static final String URL_SUPPLIER = "http://www.365jiaju.com/app/im/findfahuo.ashx";
    private static final String URL_NEARBYDETAIL = "http://www.365jiaju.com/app/im/getServiceInfo.ashx";
    private static final String URL_DINGWEI = "http://www.365jiaju.com/app/im/dingwei.ashx";
    private static final String URL_TOUSU = "http://www.365jiaju.com/app/im/tousu.ashx";


    /**
     * 注册
     * @param loginName
     * @param password
     * @return
     */
    public String register(String loginName, String password, String usertype){
//        path = URL_REGISTER;
        String redCode = "0000";
        
        try {
            JSONObject reqData = new JSONObject();
            reqData.put("loginName", loginName);
            reqData.put("password", password);
            reqData.put("usertype", usertype);
            JSONObject respObject = JSONUtil.getJSON(URL_REGISTER, reqData);
            Log.i(TAG, "respObject ==============" + respObject.toString());
            if ((respObject != null && !"".equals(respObject))) {
                if (respObject.has("status")) {
                    redCode = respObject.getString("status");
                }else if (respObject.has("uid") && respObject.get("uid") != null && !"".equals(respObject.get("uid"))){
                    FlyAntGlobalVar.loginname = respObject.getString("loginname");
                    FlyAntGlobalVar.hxacc = respObject.getString("hxacc");
                    FlyAntGlobalVar.hxpwd = respObject.getString("hxpwd");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return redCode;
        
    }
    
    /**
     * 登录
     * @param loginName
     * @param password
     * @return
     */
    public String Login(Context context, String loginName, String password) {

        String redCode = "0000";

        try {
            JSONObject reqData = new JSONObject();
            reqData.put("loginName", loginName);
            reqData.put("password", password);
            JSONObject respObject = JSONUtil.getJSON(URL_LOGIN, reqData);
            Log.i(TAG, "respObject ==============" + respObject.toString());
            if ((respObject != null && !"".equals(respObject))) {
                if (respObject.has("status")) {
                    redCode = respObject.getString("status");
                }else if (respObject.has("uid") && respObject.get("uid") != null && !"".equals(respObject.get("uid"))){
                    FlyAntGlobalVar.uid = respObject.getString("uid");
                    FlyAntGlobalVar.loginname = respObject.getString("loginname");
                    FlyAntGlobalVar.hxacc = respObject.getString("hxacc");
                    FlyAntGlobalVar.hxpwd = respObject.getString("hxpwd");
                    FlyAntGlobalVar.name = respObject.getString("name");
                    FlyAntGlobalVar.upic = respObject.getString("upic");
                    SharedPreferences preferences = context.getSharedPreferences("filename", Context.MODE_PRIVATE); 
                    Editor editor = preferences.edit();
                    editor.putString("uid", respObject.getString("uid"));
                    editor.putString("upic", respObject.getString("upic"));
                    editor.putString("loginname", respObject.getString("loginname"));
                    editor.putString("password", respObject.getString("hxpwd"));
                    editor.commit();
                }
            }
                
        } catch (Exception e) {
            Log.i(TAG, e.getMessage());
            e.printStackTrace();
        }

        return redCode;

    }
    
    /**
     * 获取用户信息
     * @param uid
     * @return
     */
    public User queryUserInfo(String uid) {
        String retCode = "";
        User user = new User();
        try {
            // 设置输入参数
            JSONObject reqData = new JSONObject();
            reqData.put("uid", uid);
            // 获取后台内容
            JSONObject respObject = JSONUtil.getJSON(URL_GETACCOUNT, reqData);
            Log.i(TAG, "respObject ==============" + respObject.toString());
            if ((respObject != null && !"".equals(respObject))) {
                if (respObject.has("status")) {
                    retCode = respObject.getString("status");
                } else if (respObject.has("uid")
                        && respObject.get("uid") != null
                        && !"".equals(respObject.get("uid"))) {
                    user.setUid(respObject.optString("uid"));
                    user.setLoginname(respObject.optString("logiNname"));
                    user.setUsername(respObject.optString("logiNname"));
                    user.setName(respObject.optString("name"));
                    if (respObject.optString("nickname") != null&& !"".equals(respObject.optString("nickname"))) {
                        user.setNickname(respObject.optString("nickname"));
                    } else {
                        user.setNickname(respObject.optString("logiNname"));
                    }

                    user.setAddress(respObject.optString("address"));
                    if (respObject.optString("sendTel") != null&& !"".equals(respObject.optString("sendTel"))) {
                        user.setSendTel(respObject.optString("sendTel"));
                    } else {
                        user.setSendTel(respObject.optString("logiNname"));
                    }
                    user.setSendAddress(respObject.optString("sendAddress"));
                    user.setComName(respObject.optString("comName"));
                    user.setUpic(respObject.optString("upic"));
                    user.setRetCode("0000");
                    FlyAntGlobalVar.upic = respObject.optString("upic");
                }
            }
        } catch (Exception e) {
            Log.e(TAG, e.getMessage());
            return null;
        }
        return user;
    }
    
    /**
     * 修改用户信息
     * @param uid
     * @param name
     * @param address
     * @param sendTel
     * @param sendAddress
     * @param comName
     * @return
     */
    public String updateUserInfo(String uid, String name,String nickname, String address, String sendTel, String sendAddress, String comName){
        String retCode = "0000";
        JSONObject reqData = new JSONObject();
        
        try {
            reqData.put("uid", uid);
            reqData.put("name", name);
            reqData.put("nickname", nickname);
            reqData.put("address", address);
            reqData.put("sendTel", sendTel);
            reqData.put("sendAddress", sendAddress);
            reqData.put("comName", comName);
            JSONObject respObject = JSONUtil.getJSON(URL_MODIFY, reqData);
            Log.i(TAG, "respObject ==============" + respObject.toString());
            if( respObject == null )
            {//网络异常
                return null;
            }
            retCode = respObject.getString( "status" );//错误返回代码
        } catch (Exception e) {
            Log.e( TAG, e.getMessage() );
            return "9999";
        }
        
        return retCode;
        
    }
    /**
     * 更新用户信息
     * @param uid
     * @param key
     * @param value
     * @return
     */
    public String updateUserInfos(String uid, String key, String value){
        String retCode = "0000";
        JSONObject reqData = new JSONObject();
        
        try {
            reqData.put("uid", uid);
            reqData.put(key, value);
            JSONObject respObject = JSONUtil.getJSON(URL_MODIFY, reqData);
            Log.i(TAG, "respObject ==============" + respObject.toString());
            if( respObject == null )
            {//网络异常
                return null;
            }
            retCode = respObject.getString( "status" );//错误返回代码
        } catch (Exception e) {
            Log.e( TAG, e.getMessage() );
            return "9999";
        }
        
        return retCode;
        
    }
    
    /**
     * 修改密码
     * @param uid
     * @param oldPwd
     * @param newPwd
     * @return
     */
    public String updatePassword(String uid, String oldPwd,String newPwd){
        String retCode = "";
        
        JSONObject reqData = new JSONObject();
        
        try {
            reqData.put("uid", uid);
            reqData.put("oldPwd", oldPwd);
            reqData.put("newPwd", newPwd);
            JSONObject respObject = JSONUtil.getJSON(URL_UPDATEPWD, reqData);
            Log.i(TAG, "respObject ==============" + respObject.toString());
            if( respObject == null )
            {//网络异常
                return null;
            }
            retCode = respObject.getString( "status" );//错误返回代码
        } catch (Exception e) {
            Log.e( TAG, e.getMessage() );
            return "9999";
        }
        
        return retCode;
    }

    /**
     * 获取联系人信息
     * @param telList
     * @return
     */
    public List<User> getConectsInfos(String telList) {
        String retCode = "";
        List<User> result = new ArrayList<User>();
        User user;
        JSONArray respObject = JSONUtil.getRequest(URL_CONTACTSINFO, telList);
        Log.i(TAG, "respObject ==============" + respObject.toString());
        if (respObject == null) {// 网络异常
            return null;
        }
        try {
            for (int i = 0; i < respObject.length(); i++) {
                user = new User();
                JSONObject json = respObject.getJSONObject(i);
                user.setUid(json.optString("uid"));
                user.setLoginname(json.optString("loginname"));
                if (json.optString("nickname") != null && !"".equals(json.optString("nickname"))) {
                    user.setNickname(json.optString("nickname"));
//                    user.setNick(json.optString("nickname"));
                }else{
                    user.setNickname(json.optString("loginname"));
                }
                user.setNick(json.optString("loginname"));
                user.setUpic(json.optString("upic"));
                result.add(user);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;

    }

    /**
     * 获取附近的人
     * @param loginnames 登录帐号
     * @param lat 纬度
     * @param lng 经度
     * @return
     */
    public List<Nearby> queryNearbyInfo(String nearbyking, String loginnames, String lat, String lng) {
        String path = "http://www.365jiaju.com/app/im/findall.ashx";
        if ("0".equals(nearbyking)) {
            path = URL_FINDALL;
        } else if ("1".equals(nearbyking)){
            path = URL_FINDDRIVER;
        } else if ("2".equals(nearbyking)){
            path = URL_FINDWULIU;
        } else if ("3".equals(nearbyking)){
            path = URL_FINDMASTER;
        } else {
            path = URL_SUPPLIER;
        }
        List<Nearby> nearbyList = new ArrayList<Nearby>();
        try {
            // 设置输入参数
            JSONObject reqData = new JSONObject();
            reqData.put("loginnames", loginnames);
            reqData.put("lat", lat);
            reqData.put("lng", lng);
            // 获取后台内容
            JSONArray respObject = JSONUtil.getNearby(path, reqData);

            Log.i(TAG, "respObject ==============" + respObject.toString());
            if (respObject == null) {// 网络异常
                return null;
            } else {
                for (int i = 0; i < respObject.length(); i++) {
                    Nearby nearby = new Nearby();
                    JSONObject json = respObject.getJSONObject(i);
                    nearby.setuId(json.optString("uid"));
                    nearby.setTel(json.optString("tel"));
                    nearby.setMingcheng(json.optString("mingcheng"));
                    nearby.setDizhi(json.optString("dizhi"));
                    nearby.setTouxiang(json.optString("touxiang"));
                    nearby.setJuli(json.optString("juli"));
                    nearby.setRedCode("0000");
                    nearbyList.add(nearby);
                }

            }
        } catch (Exception e)

        {
            Log.e(TAG, e.getMessage());
            Nearby nearby = new Nearby();
            nearby.setRedCode("9999");
            nearbyList.add(nearby);
        }

        return nearbyList;
    }

    /**
     * 附近的人详细信息
     * @param uid
     * @return
     */
    public Map<String, String> queryNearbyDetails(String uid){
        Map<String, String> nearbyInfos = new HashMap<String, String>();
        try {
            // 设置输入参数
            JSONObject reqData = new JSONObject();
            reqData.put("uid", uid);
            // 获取后台内容
            JSONObject respObject = JSONUtil.getJSON(URL_NEARBYDETAIL, reqData);
            Log.i(TAG, "respObject ==============" + respObject.toString());
            if ((respObject != null && !"".equals(respObject))) {
                if (respObject.has("status")) {
                    nearbyInfos.put("redCode", respObject.optString("status"));
                } else if (respObject.has("uid")
                        && respObject.get("uid") != null
                        && !"".equals(respObject.get("uid"))) {
                    nearbyInfos.put("uid", respObject.optString("uid"));
                    nearbyInfos.put("tel", respObject.optString("tel"));
                    nearbyInfos.put("mingcheng", respObject.optString("mingcheng"));
                    nearbyInfos.put("suozaidiqu", respObject.optString("suozaidiqu"));
                    nearbyInfos.put("touxiang", respObject.optString("touxiang"));
                    nearbyInfos.put("cartype", respObject.optString("cartype"));
                    nearbyInfos.put("zhuanxian", respObject.optString("zhuanxian"));
                    nearbyInfos.put("fuwuleixing", respObject.optString("fuwuleixing"));
                    nearbyInfos.put("redCode", "0000");
                }
            }
        } catch (Exception e) {
            Log.e(TAG, e.getMessage());
            nearbyInfos.put("redCode", "9999");
            return null;
        }
        return nearbyInfos;

    }

    /**
     * 发送位置信息
     * @param uid
     * @param lat
     * @param lng
     * @return
     */
    public String sendLocation(String uid, String lat,String lng){
        String retCode = "";

        JSONObject reqData = new JSONObject();

        try {
            reqData.put("uid", uid);
            reqData.put("lat", lat);
            reqData.put("lng", lng);
            JSONObject respObject = JSONUtil.getJSON(URL_DINGWEI, reqData);
            Log.i(TAG, "respObject ==============" + respObject.toString());
            if( respObject == null )
            {//网络异常
                return null;
            }
            retCode = respObject.getString( "status" );//错误返回代码
        } catch (Exception e) {
            Log.e( TAG, e.getMessage() );
            return "9999";
        }

        return retCode;
    }

    /**
     * 投诉
     * @param uid
     * @param tid
     * @return
     */
    public String tousu(String uid, String tid){
        String retCode = "";

        JSONObject reqData = new JSONObject();

        try {
            reqData.put("uid", uid);
            reqData.put("lat", tid);
            JSONObject respObject = JSONUtil.getJSON(URL_DINGWEI, reqData);
            Log.i(TAG, "respObject ==============" + respObject.toString());
            if( respObject == null )
            {//网络异常
                return null;
            }
            retCode = respObject.getString( "status" );//错误返回代码
        } catch (Exception e) {
            Log.e( TAG, e.getMessage() );
            return "9999";
        }

        return retCode;
    }

}
